//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "tools\fn_getLandingPos.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_center", "_radius", "_center_x", "_center_y", "_ret_val", "_co", "_angle", "_x1", "_y1", "_helper","_slope_dist","_highest","_highest_depth"];
DEFAULT_PARAM(_center,0,[]);
DEFAULT_PARAM(_radius,1,20);

_center_x = _center select 0;_center_y = _center select 1;
_ret_val = [];_co = 0;
_helper = "Land_HelipadEmpty_F" createVehicleLocal _center;
_helper setPosATL [_center_x, _center_y, 0];
_highest = [];
_highest_depth = (getPosASL _helper) select 2;
while {count _ret_val == 0 && _co < 50} do {
	_angle = floor (random 360);
	_randradius = random(_radius);
	_x1 = _center_x - (_randradius * sin _angle);
	_y1 = _center_y - (_randradius * cos _angle);
	_helper setPosATL [_x1, _y1, 0];
	_depth = (getPosASL _helper) select 2;
	TRACE_1("Check",ARR_5(_angle,_randradius,_x1,_y1,(getPosASL _helper)));
	if (_depth < 0 && _depth > -0.5) then {
		if (SurfaceIsWater [_x1, _y1]) then {
			TRACE_1("Found Spot",ARR_4(_co, _x1, _y1, _depth));
			_ret_val = [getPosASL _helper select 0, getPosASL _helper select 1,0];
		};
	};
	if (count _ret_val <= 0) then {
		if (_depth > _highest_depth) then {
			_highest_depth = _depth;
			_highest = [_x1, _y1, 0];
		};
	};
	INC(_co);
};
deleteVehicle _helper;
if (count _ret_val == 0) then {
	if (count _highest > 0) then {
		TRACE_1("Using Highest",ARR_2(_highest, _highest_depth));
		_ret_val = _highest;
	} else {
		_ret_val = _center;
	};
};
TRACE_1("End",_ret_val);
TMFPROFILERSTOP;
_ret_val;